package com.cmbchina.service;

import com.cmbchina.entity.Currency;
import com.cmbchina.entity.ExchangeRate;
import com.cmbchina.entity.Money;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 支付案例
 */
public class PayService {

    @Autowired
    private BankService bankService;

    @Autowired
    private ExchangeRateService exchangeRateService;

    /**
     * 跨境支付
     * @param money
     * @param targetCurrency
     * @param recipientId
     */
    public void pay(Money money, Currency targetCurrency, Long recipientId){
        //1、从外部系统获取汇率
        ExchangeRate rate = exchangeRateService.getRate(money.getCurrency(), targetCurrency);
        //2、根据汇率转换金额
        Money targetMoney = rate.exchange(money);
        //3、转账
        bankService.tranfer(targetMoney, recipientId);
    }

}
